//
//  ViewController.m
//  10-掌握-mapkit框架-基本使用
//
//  Created by J on 16/2/28.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "AnnoTation.h"
@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapVIew;
@property (strong, nonatomic)  CLLocationManager *manager;
@property(nonatomic,strong)CLGeocoder* geoCoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.manager = [CLLocationManager new];
    [self.manager requestAlwaysAuthorization];
    self.geoCoder = [CLGeocoder new];
    
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取当前触摸点
    CGPoint poin = [[touches anyObject] locationInView:self.mapVIew];
   CLLocationCoordinate2D pt = [self.mapVIew convertPoint:poin toCoordinateFromView:self.mapVIew];
    [self addAnnoWithPoint:pt];
  
 }


//
//- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    //移除大头针
//    NSArray* annos = self.mapVIew.annotations;
//    [self.mapVIew removeAnnotations:annos];
//    
//}



- (void)addAnnoWithPoint:(CLLocationCoordinate2D)point{
    
   __block AnnoTation* anno = [AnnoTation new];
    anno.coordinate = point;
    anno.title = @"小码哥";
    anno.subtitle = @"大神一班";
    anno.type = arc4random_uniform(5);//0-4
    [self.mapVIew addAnnotation:anno];
    CLLocation* loc = [[CLLocation alloc]initWithLatitude:anno.coordinate.latitude longitude:anno.coordinate.longitude];
    [self.geoCoder reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (!error) {
//            NSLog(@"%@",placemarks.firstObject.name);
            anno.title = placemarks.firstObject.locality;
            anno.subtitle = placemarks.firstObject.thoroughfare;
        }
        
    }];
}




//   当我们添加大头针时候
//
//   @param mapView    地图
//   @param annotation 打头针
//
//   @return 大头针视图
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
  
    MKAnnotationView* anno = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];//MKPinAnnotationView这个是系统默认的大头针视图
    if (!anno) {
        anno = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ID"];
    }

    anno.canShowCallout = YES;
    anno.annotation = annotation;
    AnnoTation* notation =(AnnoTation*)annotation;
    NSString* imageName = [NSString stringWithFormat:@"category_%zd",notation.type+1];
    anno.image = [UIImage imageNamed:imageName];
    
//    anno.calloutOffset = CGPointMake(5, 5);
    UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    imageView.image = [UIImage imageNamed:@"htl"];
    anno.leftCalloutAccessoryView = imageView;
    
    
    
    UIImageView* imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    imageView2.image = [UIImage imageNamed:@"eason"];
    anno.rightCalloutAccessoryView = imageView2;
    anno.detailCalloutAccessoryView = [UISwitch new];
    return  anno;
   
}


//系统自带大头针
- (MKPinAnnotationView*)systemAnno:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    MKPinAnnotationView* anno = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];//MKPinAnnotationView这个是系统默认的大头针视图
    if (!anno) {
        anno = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ID"];
    }
    anno.pinTintColor = [UIColor blackColor];//设置大头针颜色
    anno.animatesDrop = YES;//设置从天往下降落
    anno.canShowCallout = YES;//不论系统还是自己的大头针需要实现协议方法后这个自己设置才能出现大头针
    anno.annotation = annotation;
    //    anno.image = [UIImage imageNamed:@"1.png"];//系统的大头针 属性无效
    anno.draggable=YES;//拖拽的时候模型里面的经纬度也会变化
    return  anno;
    
}


//取消选中大头针 选中弹出气泡无效
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{
    
//    view.annotation//可以拿到大头针视图模型里面有经纬度
//    NSLog(@"%f",view.annotation.coordinate.latitude);
}

//选中大头针
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    
    
}


//更新到位置
//MKUserLocation位置对象
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    

    
    
}



@end
